package hxy.oxygen.demo.router;

import hxy.oxygen.demo.entity.Book;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vip.justlive.oxygen.core.util.net.http.HttpMethod;
import vip.justlive.oxygen.web.annotation.Mapping;
import vip.justlive.oxygen.web.annotation.Param;
import vip.justlive.oxygen.web.annotation.Router;
import vip.justlive.oxygen.web.result.Result;
import vip.justlive.oxygen.web.result.ViewResult;
import vip.justlive.oxygen.web.router.RoutingContext;

@Router("/book")
public class BookRouter {
    private static final Logger log = LoggerFactory.getLogger(BookRouter.class);

    // 视图
    @Mapping("/")
    public ViewResult index() {
        return Result.view("/book.html");
    }

    // json
    @Mapping(value = "/ajax", method = {HttpMethod.GET})
    public Book find(RoutingContext ctx) {
        // ...
        Book book = new Book();
        book.setName("Java入门学习");
        return book;
    }

    /**
     * 可以直接对象作为参数接收的
     *
     * @param book
     * @return
     */
    @Mapping(value = "/insert")
    public Book insert(@Param Book book) {
        log.info("\n====>{}", book);
        // 插入
        return book;
    }
}